//
//  XFTipView.m
//  WorkManager
//
//  Created by ukongm on 17/1/10.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "XFTipManager.h"
#import "XFTipConst.h"
#import "XFManager.h"
@interface XFTipManager()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

/** 相册控制器 */
@property(nonatomic,strong)UIImagePickerController* imagePicker;

@end

@implementation XFTipManager
{
    complate _complate; //保存回调
}

- (UIImagePickerController *)imagePicker{
    
    if (_imagePicker == nil) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    }
    return _imagePicker;
}

+ (instancetype)singletonManager{
    static dispatch_once_t onceToken;
    static XFTipManager* manager = nil;
    dispatch_once(&onceToken, ^{
        
        manager = [XFTipManager new];
        
    });
    return manager;
}

+ (XFTipView*)createShowView{
    return [[XFTipView alloc] initWithFrame:CGRectMake(100, 100, 100, XFTipView_MinHeight)];
}

+ (void)target:(UIView *)targetView{
    [self target:targetView value:@"我是有底线的"];
}

+ (void)target:(UIView*)targetView value:(NSString*)value {
    [self target:targetView value:value andShowTime:XFTipView_NormalTime];
}

+ (void)target:(UIView*)targetView value:(NSString*)value andShowTime:(CGFloat)time{
    
    [self target:targetView value:value andShowTime:time andFrameType:XFShowFrameTypeWithBottom];
}

+ (void)target:(UIView *)targetView value:(NSString *)value andFrameType:(XFShowFrameType)type{
    [self target:targetView value:value andShowTime:XFTipView_NormalTime andFrameType:type];
}

+ (void)target:(UIView *)targetView value:(NSString *)value andShowTime:(CGFloat)time andFrameType:(XFShowFrameType)type{
    
    XFTipView* tipView = [self createShowView];
    
    tipView.center = [self centerWithType:type andView:targetView];
    
    tipView.showView.text = value;
    
    [tipView changeSize:[self mathSizeForValue:value]];
    
    [targetView addSubview:tipView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(time * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [UIView animateWithDuration:0.2 animations:^{
            
            tipView.alpha = 0;
            
        } completion:^(BOOL finished) {
            [tipView removeFromSuperview];
        }];
        
    });
    
}

+ (CGPoint)centerWithType:(XFShowFrameType)type andView:(UIView*)targetView{
    
    CGPoint point;
    
    switch (type) {
        case XFShowFrameTypeWithTop:
            point = CGPointMake(targetView.width/2, targetView.height / 5.7);
            break;
        case XFShowFrameTypeWithCenter:
            point = CGPointMake(targetView.width/2, targetView.height / 2);
            break;
        case XFShowFrameTypeWithBottom:
            point = CGPointMake(targetView.width/2, targetView.height / 5 * 4);
            break;
        default:
            break;
    }
    return point;
}

+ (CGSize)mathSizeForValue:(NSString*)value{
    
    CGSize size = [value boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, XFTipView_MinHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:XFTipView_Font]} context:nil].size;
    
    size.width += XFTipView_Font * 2;
    
    if (size.width > XFTipView_MaxWidth) {
        size.width = XFTipView_MaxWidth;
    }
    
    if (size.width < XFTipView_MinWidth) {
        size.width = XFTipView_MinWidth;
    }
    
    if(size.height < XFTipView_MinHeight){
        size.height = XFTipView_MinHeight;
    }
    
    return size;
}

- (void)uploadImageForTarget:(UIViewController*)target complation:(complate)complate{
    [self uploadImageForTarget:target complation:complate edit:NO];
}

#pragma mark - 系统提示框
- (void)uploadImageForTarget:(UIViewController*)target complation:(complate)complate edit:(BOOL)edit{
    //保存下对应的回调
    _complate = complate;
    
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"" message:@"请选择" preferredStyle:UIAlertControllerStyleActionSheet];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"立刻拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        self.imagePicker.allowsEditing = edit;
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        
        [target presentViewController:self.imagePicker animated:YES completion:nil];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        self.imagePicker.allowsEditing = edit;
        self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [target presentViewController:self.imagePicker animated:YES completion:nil];
        
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [ThreadManager handleCodeForMain:^{
        [target presentViewController:alert animated:YES completion:nil];
    }];
}

#pragma mark - 选择相册的回调
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    NSString* imageUrl = UIImagePickerControllerOriginalImage;
    if (picker.allowsEditing) {
        imageUrl = UIImagePickerControllerEditedImage;
    }
    
    if (_complate) {
        _complate(info[imageUrl],info[UIImagePickerControllerReferenceURL]);
    }
}

@end

@implementation XFTipView
{
    UIView* _backView; //透明层
}
- (UITextField *)showView{
    
    if (_showView == nil) {
         UITextField* textField = [[UITextField alloc] initWithFrame:self.bounds];
        textField.font = [UIFont boldSystemFontOfSize:XFTipView_Font];
        textField.textColor = [UIColor whiteColor];
        textField.textAlignment = NSTextAlignmentCenter;
        textField.userInteractionEnabled = NO;
        textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        textField.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
        textField.leftViewMode = UITextFieldViewModeAlways;
        textField.rightViewMode = UITextFieldViewModeAlways;
        textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _showView = textField;
    }
    return _showView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.layer.cornerRadius = 3;
        self.layer.masksToBounds = YES;
        
        _backView = [[UIView alloc] initWithFrame:self.bounds];
        _backView.backgroundColor = [UIColor blackColor];
        _backView.alpha = 0.8;
        _backView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        
        [self addSubview:_backView];
        
        [self addSubview:self.showView];
    }
    return self;
}

@end
